package cn.dengta.context;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.dengta.common.context.Context;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletContextAware;

@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "webapp.context")
public class ContextImpl extends Context implements ServletContextAware {

	public ContextImpl() {
		super(true);
	}

	protected boolean fromBackstage;

	@Override
	public boolean fromBackstage(@Nullable HttpServletRequest request) {
		return fromBackstage;
	}

	@Override
	public void setServletContext(@Nonnull ServletContext sc) {
		setGlobalServletContext(sc);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
		fromBackstage = StringUtils.contains(applicationName(), "admin");
	}

}
